package com.acme.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.acme.dao.ClaimDAO;
import com.acme.entity.Claim;
import com.acme.entity.User;

@Service
public class ClaimDAOImpl implements ClaimDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	
	
	@Override
	public List<Claim> getAllClaims() {
		Query query = sessionFactory.getCurrentSession().createQuery("from Claim");
		List<?> l = query.list();
		ArrayList<Claim> claimList = new ArrayList<Claim>();
		for (Object o : l) {
			claimList.add((Claim) o);
		}
		return claimList;
	}
	
	@Override
    public Map<User, List<Claim>> getAllUserClaims() {
	Query userquery = sessionFactory.getCurrentSession().createQuery("from Claim"); //get all users
	List<?> l = userquery.list();
	Map<User, List<Claim>> userQueues = new HashMap<User, List<Claim>>();
	for(Object user : l) {
	    Query queuequery = sessionFactory.getCurrentSession().createQuery("from QueueRecord where UserID = :u"); //get queue for specific user
	    queuequery.setParameter("u", ((User) user).getUserID());
	    List<?> lp = queuequery.list();
	    ArrayList<Claim> queue = new ArrayList<Claim>();
	    for(Object claim : lp) 
	    	queue.add((Claim) claim);
	    userQueues.put((User) user, queue);
	}
	return userQueues;
    }
	
	@Override
	public Claim getClaimByID(int claimID) {
		return (Claim) sessionFactory.getCurrentSession().get(Claim.class, claimID);
	}
	@Override
	public List<Claim> getClaimsSubmmitted() {
		String querystring = "from Claim where status = 'Submitted'";
		Query query = sessionFactory.getCurrentSession().createQuery(querystring);
		//query.setParameter("Submitted", Claim.getStatus());
		List<?> l = query.list(); // query returns a raw list. 
		ArrayList<Claim> submittedClaims = new ArrayList<Claim>();
		for(Object o : l) {
		    submittedClaims.add((Claim) o);
		}
		return submittedClaims;
	}
	@Override
	public List<Claim> getAllRejectedClaims() {
		String querystring = "from Claim where status = 'Denied'";
		Query query = sessionFactory.getCurrentSession().createQuery(querystring);
		List<?> l = query.list(); // query returns a raw list. 
		ArrayList<Claim> claims = new ArrayList<Claim>();
		for(Object o : l) {
		    claims.add((Claim) o);
		}
		return claims;
	}
	
	@Override
	public List<Claim> getAllAcceptedClaims() {
		String querystring = "from Claim where status = 'Approved'";
		Query query = sessionFactory.getCurrentSession().createQuery(querystring);
		List<?> l = query.list(); // query returns a raw list. 
		ArrayList<Claim> claims = new ArrayList<Claim>();
		for(Object o : l) {
		    claims.add((Claim) o);
		}
		return claims;
	}

	@Override
	public List<Claim> getClaimsByUser(User user) {
		String querystring = "from Claim where UserID = :u order by date desc";
		Query query = sessionFactory.getCurrentSession().createQuery(querystring);
		query.setParameter("u", user.getUserID());
		List<?> l = query.list(); // query returns a raw list. 
		ArrayList<Claim> claims = new ArrayList<Claim>();
		for(Object o : l) {
		    claims.add((Claim) o);
		}
		return claims;
	}

	@Override
	public void updateClaim(Claim claim) {
		sessionFactory.getCurrentSession().merge(claim);
	}

	@Override
	public void deleteClaim(Claim claim) {
		sessionFactory.getCurrentSession().delete(claim);
	}

	/**
	 * This will use more complete filter parameters once the database contains its respective tables
	 */
	@Override
	public List<Claim> filterClaims(String status, String diagnosis, String medicareNumber) {
//		String querystring = "from Claim where status like :s and diagnosis like :d and medicareNumber like :m";
//		Query query = sessionFactory.getCurrentSession().createQuery(querystring);
//		if(status == null || status.equals("")) status = "%";
//		if(diagnosis == null || diagnosis.equals("")) diagnosis = "%";
//		if(medicareNumber == null || medicareNumber.equals("")) medicareNumber = "%";
//		query.setParameter("s", status);
//		query.setParameter("d", diagnosis);
//		query.setParameter("m", medicareNumber);
//		List<?> l = query.list(); // query returns a raw list. 
		ArrayList<Claim> claimList = new ArrayList<Claim>();
//		for(Object o : l) {
//			claimList.add((Claim) o);
//		}
		return claimList;
	}

	@Override
	public Map<Claim, List<Claim>> getClaimsSubmitted() {
		// TODO Auto-generated method stub
		return null;
	}


}
